package ui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import dao.*;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class removeWindow extends javax.swing.JFrame {
	private JPanel jPanel1;
	private AbstractAction removeCloseAction;
	private JButton removeOkButton;
	private JTextField removeScientificNameField;
	private JLabel jLabel1;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				removeWindow inst = new removeWindow();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public removeWindow() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				jPanel1 = new JPanel();
				GroupLayout jPanel1Layout = new GroupLayout((JComponent)jPanel1);
				jPanel1.setLayout(jPanel1Layout);
				getContentPane().add(jPanel1, BorderLayout.CENTER);
				jPanel1.setPreferredSize(new java.awt.Dimension(384, 97));
				{
					jLabel1 = new JLabel();
					jLabel1.setText("Scientific Name:");
				}
				{
					removeScientificNameField = new JTextField();
				}
				{
					removeOkButton = new JButton();
					removeOkButton.setText("Remove");
					removeOkButton.setAction(getRemoveCloseAction());
				}
				jPanel1Layout.setHorizontalGroup(jPanel1Layout.createSequentialGroup()
					.addContainerGap()
					.addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(jPanel1Layout.createParallelGroup()
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(removeScientificNameField, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE))
					    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					        .addGap(77)
					        .addComponent(removeOkButton, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(12, Short.MAX_VALUE));
				jPanel1Layout.setVerticalGroup(jPanel1Layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(removeScientificNameField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(jLabel1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(27)
					.addComponent(removeOkButton, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(15, Short.MAX_VALUE));
			}
			pack();
			this.setSize(337, 138);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private AbstractAction getRemoveCloseAction() {
		if(removeCloseAction == null) {
			removeCloseAction = new AbstractAction("Remove", null) {
				public void actionPerformed(ActionEvent evt) {
					String scientificName = removeScientificNameField.getText();
					boolean notfound = false;
					
					try {
						BirdVO b = BirdDAO.findByScientificName(scientificName);
						if (b == null) {
							notfound = true;
						} else
							System.out.println("Bird" + b.getScientific_name());
						BirdDAO.removeBird(b);
					} catch (SQLException e1) {
						notfound = true;
						// TODO Auto-generated catch block
						System.out.println(e1.getMessage());
					}
					
					successDialog sD = new successDialog(removeWindow.this);
					sD.setLocationRelativeTo(null);
					sD.setVisible(true);
					
					if ( removeScientificNameField.getText().length() > 0 ) {
						if ( notfound ) {
							sD.setLabelText("Registry " + removeScientificNameField.getText() + " does not exist!");
						} else {
							sD.setLabelText("Registry " + removeScientificNameField.getText() + " removed!");
						}
					} else {
						sD.setLabelText("Scientific name field is empty!");
					}
					
				}
			};
		}
		return removeCloseAction;
	}

}
